
define(['jquery', 'bootstrap', 'frontend', 'form', 'template', 'cookie', 'frontend/password_verified'], function ($, undefined, Frontend, Form, Template, cookie_class, PasswordVerified) {
    var subject_class_id = 0;
    var unique_id = 0;
    var library_sound = 0;
    var library_video = 0;
    var library_game = 0;

    //1为课件区，2视频区 3游戏区
    var find_area_all_flag = 1;

    var type = 0;
    var area = 0;
    var area2 = 0;
    var version = 0;
    var year = 0;
    var level = 0
    var knowledge = 0;
    var experiment = 0;
    var sortby_index = 1;

    var page = 1;
    var page_size = 10;

    var keywords = "";

    var is_online = 0;
    var is_record = 0;

    var Controller = {
        index: function () {

            //初始化密码验证
            PasswordVerified.init();

            Controller.bindSubjectClassMenuEvent();
            Controller.bindVideoMenuEvent();
            Controller.bindGameMenuEvent();

            Controller.bindConditionClickEvent();

            Controller.bindExperimentItemEvent();
            Controller.bindKnowledgeItemEvent();
            Controller.fetchSubjectAttachment();

            //回车键
            document.onkeydown = function (event) {
                if (event.keyCode === 13 || event.which === 13) {
                    Controller.startSearch();
                    return false;
                }
            };

            //点击查找
            $(".file-list-header .search-btn").click(function () {
                Controller.startSearch();
                return false;
            });
        },
        //搜索关键词
        startSearch: function () {
            keywords = $(".search-keyword").val();

            page = 1;
            Controller.fetchSubjectAttachment();
        },
        //绑定科目分类点击事件
        bindSubjectClassMenuEvent: function () {
            //头部点击，查找该区所有附件
            $(".subject-class-body .main-menu-header").on("click", function () {

                var index = $(this).data("index");
                $(".main-menu-header").each(function () {
                    var index2 = $(this).data("index");
                    if (index != index2) {
                        $(this).removeClass("main-menu-header-focus");
                    }
                });

                $(this).hasClass("main-menu-header-focus") ? $(this).removeClass("main-menu-header-focus") : $(this).addClass("main-menu-header-focus")

                $(".file-sort-condition:eq(0)").removeClass("file-sort-condition-focus");
                if ($(this).hasClass("main-menu-header-focus")) {
                    $(".file-sort-condition:eq(0)").addClass("file-sort-condition-focus");
                }

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            //从地址栏带过来的参数，自动展开对应的科目
            if (Config.unique_id != "") {

                unique_id = Config.unique_id;
                //当前节点
                subject_class_id = $(".subject-class-body [unique_id='" + unique_id + "']").attr("id");
                console.log("subject_class_id", subject_class_id);
                $(".subject-class-body [id='" + subject_class_id + "']").addClass("menu-focus");
                $(".subject-class-body [id='" + subject_class_id + "']").data("status", "open");

                //向下箭头，向下的列表
                $(".subject-class-body [dropdown-unique-id='" + unique_id + "']").addClass("rotate");
                $(".subject-class-body [id='menu-1-items-" + subject_class_id + "']").show();
                $(".subject-class-body [id='menu-items-" + subject_class_id + "']").show();

                //向上的元素
                $(".subject-class-body [unique_id='" + unique_id + "']").parentsUntil(".subject-class-body  .main-menu-body").show();
                $(".subject-class-body [unique_id='" + unique_id + "']").parentsUntil(".subject-class-body  .main-menu-body").each(function () {
                    var top_dropdown = $(this).attr("top-dropdown");
                    if (top_dropdown !== undefined) {
                        console.log(top_dropdown);
                        $("#" + top_dropdown).addClass("rotate");

                        //顶级元素
                        var top_menu_id = $(this).attr("top_menu_id");
                        $(".subject-class-body [id='" + top_menu_id + "']").data("status", "open");
                    }
                });

                Controller.fetchSubjectAttachment();
            }

            $(".subject-class-body .menu-1-title").on("click", function () {

                var id = $(this).attr("id");

                //关闭同级其他一级菜单
                $(".subject-class-body .menu-1-title").each(function () {
                    var id2 = $(this).attr("id");
                    if (id != id2) {
                        $(this).removeClass("menu-focus");
                        $(".subject-class-body #menu-1-items-" + id2).css("display", "none");
                        $(".subject-class-body #menu-1-dropdown-" + id2).removeClass("rotate");
                    }
                });

                if ($(this).hasClass("menu-focus")) {
                    $(this).removeClass("menu-focus");
                    subject_class_id = 0;
                    $(".subject-class-body #menu-1-items-" + id).css("display", "none");
                    $(".subject-class-body #menu-1-dropdown-" + id).removeClass("rotate");
                } else {
                    $(this).addClass("menu-focus");
                    subject_class_id = id;
                    $(".subject-class-body #menu-1-items-" + id).css("display", "block");
                    $(".subject-class-body #menu-1-dropdown-" + id).addClass("rotate");

                    //查找知识点
                    Controller.getKnowledgeList();
                    //查找实验点
                    Controller.getExperimentList();
                }

                console.log("subject_class_id", subject_class_id);
                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".subject-class-body .menu-title").on("click", function () {
                var id = $(this).attr("id");

                $(".subject-class-body .menu-title").removeClass("menu-focus");
                $(this).addClass("menu-focus");
                $(".subject-class-body #menu-items-" + id).css("display") == "block" ? $(".subject-class-body  #menu-items-" + id).css("display", "none") : $(".subject-class-body  #menu-items-" + id).css("display", "block");

                subject_class_id = id;
                console.log("subject_class_id", subject_class_id);

                //查找知识点
                Controller.getKnowledgeList();
                //查找实验点
                Controller.getExperimentList();

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });
        },
        //绑定视频区分类点击事件
        bindVideoMenuEvent: function () {
            //头部点击，查找该区所有附件
            $(".library-video-body .main-menu-header").on("click", function () {

                var index = $(this).data("index");
                $(".main-menu-header").each(function () {
                    var index2 = $(this).data("index");
                    if (index != index2) {
                        $(this).removeClass("main-menu-header-focus");
                    }
                });
                $(this).hasClass("main-menu-header-focus") ? $(this).removeClass("main-menu-header-focus") : $(this).addClass("main-menu-header-focus")

                $(".file-sort-condition:eq(0)").removeClass("file-sort-condition-focus");

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".library-video-body .menu-1-title").on("click", function () {
                var id = $(this).attr("id");
                //关闭同级其他一级菜单
                $(".library-video-body .menu-1-title").each(function () {
                    var id2 = $(this).attr("id");
                    if (id != id2) {
                        $(this).removeClass("menu-focus");
                        $(".library-video-body #menu-1-items-" + id2).css("display", "none");
                        $(".library-video-body #menu-1-dropdown-" + id2).removeClass("rotate");
                    }
                });

                if ($(this).hasClass("menu-focus")) {
                    $(this).removeClass("menu-focus");
                    library_video = 0;
                    $(".library-video-body #menu-1-items-" + id).css("display", "none");
                    $(".library-video-body #menu-1-dropdown-" + id).removeClass("rotate");
                } else {
                    $(this).addClass("menu-focus");
                    library_video = id;
                    $(".library-video-body #menu-1-items-" + id).css("display", "block");
                    $(".library-video-body #menu-1-dropdown-" + id).addClass("rotate");

                }

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".library-video-body .menu-title").on("click", function () {
                var id = $(this).attr("id");
                $(".library-video-body .menu-title").removeClass("menu-focus");
                $(this).addClass("menu-focus");
                $(".library-video-body #menu-items-" + id).css("display") == "block" ? $(".library-video-body #menu-items-" + id).css("display", "none") : $(".library-video-body #menu-items-" + id).css("display", "block");

                library_video = id;
                console.log("library_video", library_video);

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });
        },
        //绑定游戏区分类点击事件
        bindGameMenuEvent: function () {
            //头部点击，查找该区所有附件
            $(".library-game-body .main-menu-header").on("click", function () {

                var index = $(this).data("index");
                $(".main-menu-header").each(function () {
                    var index2 = $(this).data("index");
                    if (index != index2) {
                        $(this).removeClass("main-menu-header-focus");
                    }
                });
                $(this).hasClass("main-menu-header-focus") ? $(this).removeClass("main-menu-header-focus") : $(this).addClass("main-menu-header-focus")

                $(".file-sort-condition:eq(0)").removeClass("file-sort-condition-focus");

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".library-game-body .menu-1-title").on("click", function () {
                var id = $(this).attr("id");
                //关闭同级其他一级菜单
                $(".library-game-body .menu-1-title").each(function () {
                    var id2 = $(this).attr("id");
                    if (id != id2) {
                        $(this).removeClass("menu-focus");
                        $(".library-game-body #menu-1-items-" + id2).css("display", "none");
                        $(".library-game-body #menu-1-dropdown-" + id2).removeClass("rotate");
                    }
                });

                if ($(this).hasClass("menu-focus")) {
                    $(this).removeClass("menu-focus");
                    library_game = 0;
                    $(".library-game-body #menu-1-items-" + id).css("display", "none");
                    $(".library-game-body #menu-1-dropdown-" + id).removeClass("rotate");
                } else {
                    $(this).addClass("menu-focus");
                    library_game = id;
                    $(".library-game-body #menu-1-items-" + id).css("display", "block");
                    $(".library-game-body #menu-1-dropdown-" + id).addClass("rotate");

                }

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".library-game-body .menu-title").on("click", function () {
                var id = $(this).attr("id");

                $(".library-game-body .menu-title").removeClass("menu-focus");
                $(this).addClass("menu-focus");
                $(".library-game-body #menu-items-" + id).css("display") == "block" ? $(".library-game-body  #menu-items-" + id).css("display", "none") : $(".library-game-body #menu-items-" + id).css("display", "block");

                library_game = id;
                console.log("library_game", library_game);

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });
        },
        //绑定条件分类点击事件
        bindConditionClickEvent: function () {
            $(".library-type-item").on("click", function () {
                type = $(this).data("id");
                console.log("type", type);

                $(".library-type-item").removeClass("condition-content-item-focus");
                $(this).addClass("condition-content-item-focus");

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".library-area-item").on("click", function () {
                area = $(this).data("id");
                console.log("area", area);

                $(".library-area-item").removeClass("condition-content-item-focus");
                $(this).addClass("condition-content-item-focus");

                //重置二级地区
                area2 = 0;
                $(".library-area2-item").removeClass("condition-content-item-focus");

                $(".area-children").hide();
                $("[area-children-id='" + area + "']").show();

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".library-area2-item").on("click", function () {
                area2 = $(this).data("id");
                console.log("area2", area2);

                $(".library-area2-item").removeClass("condition-content-item-focus");
                $(this).addClass("condition-content-item-focus");

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;
            });

            $(".library-version-item").on("click", function () {
                version = $(this).data("id");
                console.log("version", version);

                $(".library-version-item").removeClass("condition-content-item-focus");
                $(this).addClass("condition-content-item-focus");

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;

            });

            $(".library-year-item").on("click", function () {
                year = $(this).data("id");
                console.log("year", year);

                $(".library-year-item").removeClass("condition-content-item-focus");
                $(this).addClass("condition-content-item-focus");

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;

            });

            $(".library-level-item").on("click", function () {
                level = $(this).data("id");
                console.log("level", level);

                $(".library-level-item").removeClass("condition-content-item-focus");
                $(this).addClass("condition-content-item-focus");

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;

            });

            $(".condition-content-item-knowledge").mouseenter(function () {
                $(".knowledge-pop-body").show();
            });
            $(".condition-content-item-knowledge").mouseleave(function () {
                $(".knowledge-pop-body").hide();
            });

            $(".condition-content-item-experiment").mouseenter(function () {
                $(".experiment-pop-body").show();
            });
            $(".condition-content-item-experiment").mouseleave(function () {
                $(".experiment-pop-body").hide();
            });

            $(".file-sort-condition").on("click", function () {
                sortby_index = $(this).data("index");
                console.log("sortby_index", sortby_index);

                $(".file-sort-condition").removeClass("file-sort-condition-focus");
                $(this).addClass("file-sort-condition-focus");


                if (sortby_index == 1) {
                    $(".subject-class-body .main-menu-header").addClass("main-menu-header-focus");
                    $(".library-video-body .main-menu-header").removeClass("main-menu-header-focus");
                    $(".library-game-body .main-menu-header").removeClass("main-menu-header-focus");
                    find_area_all_flag = 1;
                    is_online = 0;
                    is_record = 0;
                }

                if (sortby_index == 2 || sortby_index == 3) {
                    $(".subject-class-body .main-menu-header").removeClass("main-menu-header-focus");
                    $(".library-video-body .main-menu-header").removeClass("main-menu-header-focus");
                    $(".library-game-body .main-menu-header").removeClass("main-menu-header-focus");
                    find_area_all_flag = 0;
                    is_online = 0;
                    is_record = 0;
                }

                if (sortby_index == 4 || sortby_index == 5) {

                    $(".subject-class-body .main-menu-header").removeClass("main-menu-header-focus");

                    $(".library-video-body .main-menu-header").addClass("main-menu-header-focus");

                    $(".library-game-body .main-menu-header").removeClass("main-menu-header-focus");

                    find_area_all_flag = 2;
                    if (sortby_index == 4) {
                        is_online = 1;
                        is_record = 0
                    }
                    if (sortby_index == 5) {
                        is_online = 0;
                        is_record = 1;
                    }
                }

                page = 1;
                Controller.fetchSubjectAttachment();
                return false;

            });
        },
        createKnowledgeItem: function (row) {
            console.log("row", row);
            var html = "";
            html += '<div class="pop-row" data-id="' + row.id + '" data-name="' + row.title + '" >';
            html += '<div class="pop-row-name" >' + row.title;
            if (row.children != null) {
                if (row.children.length > 0) {
                    html += '<span class="right-icon"><i class="fa fa-angle-right"></i></span>';
                }
            }
            html += '</div>';
            if (row.children != null) {
                if (row.children.length > 0) {
                    for (var i = 0; i < row.children.length; i++) {
                        html += '<div class="pop-row-children" children-id="' + row.id + '" >' + row.title;
                        html += Controller.createKnowledgeItem(row.children[i]);
                        html += '</div>';
                    }
                }
            }
            html += '</div>';
            return html;
        },
        getKnowledgeList: function () {

            $(".knowledge-name").html("知识点");
            $(".knowledge-pop-body").hide();
            $(".knowledge-pop-body").html("");

            $.ajax({
                url: '/index/library/getKnowledgeList',
                type: 'post',
                data: {
                    class_id: subject_class_id,
                },
                success: function (json) {
                    console.log(json);
                    var html = ' <div class="pop-row" data-id="0" data-name="知识点" ><div class="pop-row-name" >不限</div></div>';
                    if (json["code"] == 1) {
                        var list = json["list"];
                        for(var i=0;i<list.length;i++){
                            var e=list[i];
                            html += Controller.createKnowledgeItem(e);
                        }
                    }
                    $(".knowledge-pop-body").html(html);
                    Controller.bindKnowledgeItemEvent();
                }
            });
        },
        getExperimentList: function () {

            $(".experiment-name").html("实验");
            $(".experiment-pop-body").hide();
            $(".experiment-pop-body").html("");

            $.ajax({
                url: '/index/library/getExperimentList',
                type: 'post',
                data: {
                    class_id: subject_class_id,
                },
                success: function (json) {
                    console.log(json);
                    var html = ' <div class="pop-row" data-id="0" data-name="实验" ><div class="pop-row-name" >不限</div></div>';
                    if (json["code"] == 1) {
                        var list = json["list"];
                        for(var i=0;i<list.length;i++){
                            var e=list[i];
                            html += Controller.createKnowledgeItem(e);
                        }
                    }
                    $(".experiment-pop-body").html(html);
                    Controller.bindExperimentItemEvent();
                }
            });
        },
        bindKnowledgeItemEvent: function () {
            $(".knowledge-pop-body .pop-row").mouseenter(function () {
                var id = $(this).data("id");
                console.log("id", id);

                $(this).find(".pop-row-children").hide();
                $(".knowledge-pop-body  [children-id='" + id + "']").show();
                return false;
            });

            $(".knowledge-pop-body .pop-row").mouseleave(function () {
                var id = $(this).data("id");
                console.log("id", id);
                $(this).find(".pop-row-children").hide();
                return false;
            });

            $(".knowledge-pop-body .pop-row").click(function () {
                var id = $(this).data("id");
                var name = $(this).data("name");
                console.log("id", id);
                console.log("name", name);

                knowledge = id;
                $(".knowledge-name").html(name);
                $(".knowledge-pop-body .pop-row-children").hide();
                $(".knowledge-pop-body").hide();

                Controller.fetchSubjectAttachment();
                return false;
            });
        },
        bindExperimentItemEvent: function () {
            $(".experiment-pop-body .pop-row").mouseenter(function () {
                var id = $(this).data("id");
                console.log("id", id);

                $(this).find(".pop-row-children").hide();
                $(".experiment-pop-body  [children-id='" + id + "']").show();
                return false;
            });

            $(".experiment-pop-body .pop-row").mouseleave(function () {
                var id = $(this).data("id");
                console.log("id", id);
                $(this).find(".pop-row-children").hide();
                return false;
            });

            $(".experiment-pop-body .pop-row").click(function () {
                var id = $(this).data("id");
                var name = $(this).data("name");
                console.log("id", id);
                console.log("name", name);

                experiment = id;
                $(".experiment-name").html(name);
                $(".experiment-pop-body .pop-row-children").hide();
                $(".experiment-pop-body").hide();

                Controller.fetchSubjectAttachment();
                return false;
            });
        },
        fetchSubjectAttachment: function () {

            //是否在查找左侧大区
            find_area_all_flag = 0;
            $(".main-menu-header").each(function () {
                var index = $(this).data("index");
                if ($(this).hasClass("main-menu-header-focus")) {
                    find_area_all_flag = index;
                }
            });

            Controller.showLoading();

            $.ajax({
                url: '/index/library/fetchSubjectAttachment',
                type: 'post',
                data: {
                    class_id: subject_class_id,
                    library_sound: library_sound,
                    library_video: library_video,
                    library_game: library_game,
                    type: type,
                    area: area,
                    area2: area2,
                    version: version,
                    year: year,
                    level: level,
                    knowledge: knowledge,
                    experiment: experiment,
                    sortby_index: sortby_index,
                    page: page,
                    page_size: page_size,
                    find_area_all_flag: find_area_all_flag,
                    keywords: keywords,
                    is_online: is_online,
                    is_record: is_record,
                },
                success: function (json) {
                    // console.log(json);
                    var attachment_list = json["list"];
                    totalItems = json["count"];

                    var html = "";
                    $.each(attachment_list,  function (i, e){

                        html += '<div class="file-row">';
                        html += '<div class="file-item-image">';
                        if (e.library_level == 2) {
                            html += ' <div class="category normal-tag">普通</div>';
                        }
                        if (e.library_level == 3) {
                            html += '  <div class="category boutique-tag">精品</div>';
                        }
                        if (e.library_level == 4) {
                            html += ' <div class="category special-tag">特供</div>';
                        }
                        html += Controller.thumb(e);
                        html += '</div>';
                        html += '<div class="file-item-info">';
                        var title = "";
                        if (e.filename_new != "") {
                            title = e.filename_new;
                        } else {
                            title = e.filename;
                        }
                        html += '<div class="file-item-info-title"><a href="detail?id=' + e.unique_id + '">' + title + '</a></div>';
                        html += '<div class="file-item-info-tags">';
                        var html2 = "";
                        if(e.tags_array.length>0){
                            $.each(e.tags_array,  function (i, e2){
                                html2 += '<div class="tag-lable">' + e2 + '</div>';
                            });
                        }
                        html += html2;
                        html += '</div>';

                        html += '<div class="file-item-info-introduce" title="简介：' + (e.introduce ? e.introduce : "") + '">简介：' + (e.introduce ? e.introduce : "") + '</div>';
                        html += '<div class="file-item-info-more">';
                        html += '<div class="info-pv">浏览量：' + e.pv + '</div>';
                        html += '<div class="info-download">下载量：' + e.year_download + '</div>';
                        if (e.subject_class_titles2 != null && e.subject_class_titles2 != "") {
                            html += '<div class="info-subject-class-titles" title="' + e.subject_class_titles2 + '">科目：' + e.subject_class_titles2 + '</div>';
                        }
                        html += '</div>';
                        html += '</div>';

                        html += '<div class="file-item-handle">';

                        html += '<div class="handle-view">';
                        html += '<div unique_id="' + e.unique_id + '" data-filename_new="' + e.filename_new + '" data-filename="' + e.filename_new + '"  class="view-btn">查看</div>';
                        if (e.right_type == 1) {
                            html += '<div unique_id="' + e.unique_id + '" data-filename_new="' + e.filename_new + '" data-filename="' + e.filename_new + '" class="download-btn-disable">下载</div>';
                        } else {
                            html += '<div unique_id="' + e.unique_id + '" data-filename_new="' + e.filename_new + '" data-filename="' + e.filename_new + '"  class="download-btn" >下载</div>';
                        }
                        html += '</div>';

                        if (e.is_classroom == 1 && e.subject_class_unique_ids != null && e.subject_class_unique_ids != "") {
                            html += '<div class="handle-view classroom" id="' + e.id + '" >';
                            html += '<div subject_class_ids="' + e.subject_class_ids + '" subject_class_titles="' + e.subject_class_titles + '" subject_class_unique_ids="' + e.subject_class_unique_ids + '" class="classroom-btn">进入教室</div>';
                        } else {
                            html += '<div class="handle-view classroom-disable" id="' + e.id + '" >';
                            html += '<div subject_class_ids="' + e.subject_class_ids + '" subject_class_titles="' + e.subject_class_titles + '" subject_class_unique_ids="' + e.subject_class_unique_ids + '" class="classroom-btn-disable">进入教室</div>';
                        }
                        if (e.subject_class_unique_ids != null && e.subject_class_unique_ids != "") {
                            html += '<div class="classroom-pop-body" id="classroom_pop_body_' + e.id + '">';
                            var unique_id_array = e.subject_class_unique_ids.split(",");
                            var titles_array = e.subject_class_titles.split(",");
                            for (var i = 0; i < unique_id_array.length; i++) {
                                if (unique_id_array[i] != "") {
                                    html += '<div class="classroom-row" title="' + titles_array[i] + '" unique_id="' + unique_id_array[i] + '" >' + titles_array[i] + '</div>';
                                }
                            }
                            console.log(unique_id_array);
                            html += '</div>';
                        }
                        html += '</div>';

                        html += '<div class="handle-view">';
                        html += '<div class="collect-btn" subject_class_id="' + e.subject_class_id + '" unique_id="' + e.unique_id + '" >+收藏</div>';
                        html += '<div class="fee-info">';
                        if (e.right_type == 2) {
                            html += e.fee + ' 储值';
                        }
                        else if (e.right_type == 3) {
                            html += '免费';
                        }
                        html += '</div>';
                        html += '</div>';

                        html += '</div>';

                        html += '</div>';

                    });


                    $("#attachment_list").html(html);
                    $(".total-item").html(totalItems);

                    $(".view-btn").click(function () {
                        var unique_id = $(this).attr("unique_id");
                        var filename_new = $(this).data("filename_new");
                        var filename = $(this).data("filename");
                        var title = filename;
                        if (filename_new != "") {
                            title = filename_new;
                        }
                        Controller.openViewWindow(unique_id, title);
                    })

                    $(".download-btn").click(function () {
                        var unique_id = $(this).attr("unique_id");
                        var url = Fast.api.fixurl("/index/library/download?id=" + unique_id);
                        console.log(unique_id, url);
                        window.open(url, "_blank");
                    })

                    $(".classroom").mouseenter(function () {
                        var id = $(this).attr("id");
                        $("#classroom_pop_body_" + id).show();
                    })

                    $(".classroom").mouseleave(function () {
                        var id = $(this).attr("id");
                        $("#classroom_pop_body_" + id).hide();
                    })

                    $(".classroom-row").click(function () {
                        var unique_id = $(this).attr("unique_id");
                        var url = Fast.api.fixurl("/index/classroom/index?unique_id=" + unique_id);
                        console.log(unique_id, url);
                        window.open(url, "_blank");
                    });


                    var totalPages = Math.ceil(totalItems / page_size);
                    console.log("page_size", page_size);
                    console.log("totalItems", totalItems);
                    console.log("totalPages", totalPages);

                    //分页脚
                    Controller.renderPagination(page, totalPages);
                }
            });
        },
        //打开查看窗口
        openViewWindow: function (unique_id, title) {
            Fast.api.open("/index/library/pop?id=" + unique_id, title, {
                shadeClose: false,
                shade: [0.7, '#393D49'],
                area: ['80%', '80%'],
                maxmin: false,
                // closeBtn: 0,
                callback: function (value) {
                    console.log(value);
                }
            });
        },
        showLoading: function () {
            var html = '<div class="loading"><img src="/assets/img/loading.gif"></div>';
            $("#attachment_list").html(html);
        },
        thumb: function (row) {
            var html = '';
            html = ' <div ><img src="' + Fast.api.fixurl("/index/ajax/icon") + "?suffix=" + row.imagetype + '"></div>';
            return html;
        },
        renderPagination: function (page, numPages) {
            var html = '<ui class="pagination">';
            for (let i = 1; i <= numPages; i++) {
                if (i == page) {
                    html += '<li class="active" data-page=' + i + ' >' + i + '</li>';
                } else {
                    html += '<li data-page=' + i + ' >' + i + '</li>';
                }
            }
            html += '</ui>';
            $(".pager").html(html);

            $(".pager li").click(function () {
                var p = $(this).data("page");
                Controller.changePage(p);
            });
        },
        changePage: function (p) {
            page = p;
            console.log("page", page);
            Controller.fetchSubjectAttachment();
        },
        //附件详情页
        detail: function () {
            Controller.openFile(row);

            $(".download-btn").click(function () {
                var unique_id = $(this).attr("unique_id");
                var url = Fast.api.fixurl("/index/library/download?id=" + unique_id);
                console.log(unique_id, url);
                window.open(url, "_blank");
            });
        },
        //弹出窗口
        pop: function () {
            Controller.openFile(row);
        },
        openFile: function (row) {
            if (row.imagetype == "png" || row.imagetype == "jpg" || row.imagetype == "jpeg" || row.imagetype == "gif") {
                Controller.showImage(row);
                return;
            }

            if (row.imagetype == "pdf") {
                Controller.showPDF(row);
                return;
            }

            if (row.imagetype == "pptx" || row.imagetype == "pptx" || row.imagetype == "doc" || row.imagetype == "docx" || row.imagetype == "xls" || row.imagetype == "xlsx") {
                Controller.showOffice(row);
                return;
            }


            if (row.imagetype == "mp4") {
                Controller.showMP4(row);
                return;
            }

            window.open(row.url);
            return;
        },
        showMP4: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "none");

            var str = '<video-js id="fullscreen_video"  controls preload="auto"  ><source src="' + url + '" type="video/mp4"></source></video-js>';
            // var str = '<video id="fullscreen_video"  controls   ><source src="' + url + '" type="video/mp4"> 您的浏览器不支持HTML5视频。</video>';

            $(".mid-body-top").html(str);

            const player = videojs('fullscreen_video', {
                playbackRates: [0.5, 1, 1.5, 2]
            });

            player.ready(function () {
                console.log("player ready");
                player.volume(0.2);

                var playbackTime = $.cookie(url);
                var duration = $.cookie(url + "_duration");
                console.log("playbackTime", playbackTime);
                console.log("duration", duration);

                if (parseFloat(playbackTime) < parseFloat(duration)) {
                    console.log("last played did not finished");
                    player.currentTime(parseFloat(playbackTime));
                } else {
                    console.log("no played or finished");
                }
            });

            var playing = false;

            // 监听播放事件
            player.on('play', function () {
                console.log('Video is playing');
                playing = true;
            });

            // 监听暂停事件
            player.on('pause', function () {
                console.log('Video is paused');
                playing = false;
            });

            // 监听timeupdate事件
            player.on('timeupdate', function () {
                if (playing) {
                    console.log("timeupdate");

                    var playbackTime = player.currentTime(); // 获取当前播放时间
                    console.log("playbackTime", playbackTime);

                    var duration = player.duration(); // 获取视频总时长
                    console.log("duration", duration);

                    $.cookie(url + "_duration", duration + "", { expires: 365, path: "/" });
                    //没有播完
                    if (playbackTime < duration) {
                        $.cookie(url, playbackTime + "", { expires: 365, path: "/" });
                    } else {
                        $.cookie(url, null);
                        $.cookie(url + "_duration", null);
                    }
                }
            });

            const playerContainer = document.getElementById('fullscreen_video');
            playerContainer.oncontextmenu = function (event) {
                // 阻止默认的右键菜单显示
                event.preventDefault();
            };

        },
        showImage: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "none");

            var str = '<img id="fullscreen_img"  src="' + url + '"  />';
            $(".mid-body-top").html(str);

            $("#fullscreen_img").click(function () {
                Controller.allScreen("fullscreen_img");
            });

        },
        showPDF: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "block");


            // var str = '<iframe  id="fullscreen_pdf"  src="/pdfj/web/viewer.html?file=' + url + '" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"  ></iframe>';
            //     $(".mid-body-top").html(str);

            PDFObject.embed(url, ".mid-body-top");

            $(".loading").css("display", "none");


            // $('#fullscreen_pdf').on('load', function () {
            //     // iframe加载完成后的操作
            //     console.log('iframe加载完成');
            //     $(".loading").css("display", "none");

            // });
        },
        showOffice: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "block");

            // var url1 = "http://feng.eucms.net/classroom" + url + "&wdAr=1.7777777777777777";
            var url1 = "http://8.134.104.50:8088" + url;
            var url2 = encodeURI(url1);
            // var str = '<iframe id="my-iframe"  src="https://view.officeapps.live.com/op/embed.aspx?src=' + url2 + '" >正在加载文档，请等待。。。。。</iframe>';
            var str = '<iframe id="fullscreen_office"   src="https://vip.ow365.cn/?i=34482&furl=' + url2 + '" >正在加载文档，请等待。。。。。</iframe>';
            $(".mid-body-top").html(str);

            $('#fullscreen_office').on('load', function () {
                // iframe加载完成后的操作
                console.log('iframe加载完成');
                $(".loading").css("display", "none");
            });
        },
    };
    return Controller;
});
